package com.orangelife.mobile.property.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.MoneyUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.common.jump.JumpManager;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.property.bean.PropertyFeeListEntity;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 物业缴费的小区对应的缴费项
 * Created by ${shz} on 2015/12/3.Modify by wgf on 160519.
 *
 * @author shz
 */
public class PropertyBillHouseListAdapter<T> extends FatherAdapter<T> {

    private Context mContext;
    private List<PropertyFeeListEntity> mList;

    public PropertyBillHouseListAdapter(Context context, List<PropertyFeeListEntity> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseAdapter setList(List<T> list) {
        this.mList = (List<PropertyFeeListEntity>) list;
        return this;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_property_history, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(R.id.tv1, viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag(R.id.tv1);
        }

        PropertyFeeListEntity entity = mList.get(position);
        String mCommID = entity.getCommID();
        String mComm = entity.getHouseInfo();
        String apartmentID = entity.getApartmentID();
        String mPmfeeTime = entity.getRange();
        String billType = entity.getBillType();
        String totalMoney = entity.getTotalamount();
        viewHolder.tvMoney.setText(MoneyUtil.handleMoney(totalMoney) + "元");
        viewHolder.tvCommunity.setText(mComm);
        viewHolder.tvDuration.setText(mPmfeeTime);
        viewHolder.rlItem.setTag(R.id.rlItem, position);
        viewHolder.rlItem.setTag(R.id.tvDuration, billType);
        viewHolder.rlItem.setTag(R.id.tvMoney, apartmentID);
        viewHolder.rlItem.setTag(R.id.tvCommunity, mCommID);
        viewHolder.rlItem.setOnClickListener(listener);
        viewHolder.rlItem.setOnLongClickListener(longClickListener);
        return convertView;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag(R.id.rlItem);
            String billType = (String) v.getTag(R.id.tvDuration);
            if (!PropertyHttpBiz.isNewBillType(billType)) {
                String mCommID = (String) v.getTag(R.id.tvCommunity);
                String apartmentID = (String) v.getTag(R.id.tvMoney);
                jumpToOldDetail(mCommID, apartmentID);
            } else {
                jumpToDetail(pos);
            }
        }
    };

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            int pos = (int) v.getTag(R.id.rlItem);
            showConfirmDialog(pos);
            return true;
        }
    };

    /** 旧版 */
    private void jumpToOldDetail(String commID, String apartmentID) {
        JumpManager.jumpToOldProperty(mContext, commID, apartmentID);
    }

    /** 新版 */
    private void jumpToDetail(int pos) {
        /*将PropertyBillHouseListActivity加入Paid列表，以保证支付成功后可以关闭PropertyBillHouseListActivity。
        若不关闭，则在支付跳转到PropertyBillHouseListActivity时，由于该Activity未被销毁，则新的Intent信息不会生效*/
        ExitApplication.getInstance().addPaidActivity((Activity) mContext);
        JumpManager.jumpToPropertyFeeDetail(mList.get(pos).getCommID(), mList.get(pos).getApartmentID(), "0", mContext);
    }

    private void showConfirmDialog(final int position) {
        final ConfirmDialog dialog = new ConfirmDialog(mContext, "提示", "确定要删除吗",
                "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                deleteRecord(position);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void deleteRecord(final int position) {
        PropertyHttpBiz.delFee(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                if (OrangeErrorHandler.getInstance().isSuccess(response, "删除失败", "删除成功")) {
                    mList.remove(position);
                    notifyDataSetChanged();
                }
            }
        }, mList.get(position).getCommID(), mList.get(position).getApartmentID());
    }

    static class ViewHolder {
        @Bind(R.id.tvCommunity)
        TextView tvCommunity;
        @Bind(R.id.llCommunity)
        RelativeLayout rlCommunity;
        @Bind(R.id.tvMoney)
        TextView tvMoney;
        @Bind(R.id.tvDuration)
        TextView tvDuration;
        @Bind(R.id.rlItem)
        LinearLayout rlItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
